/**
 * 
 */
package com.scpii.universal.struts.manage;

import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.scpii.universal.action.BaseAction;
import com.scpii.universal.constant.GenericConstants;
import com.scpii.universal.dto.ClientMoreDto;
import com.scpii.universal.hibernate.entity.AppUpdateTab;
import com.scpii.universal.service.AppIconService;
import com.scpii.universal.service.AppUpdateService;
import com.scpii.universal.service.MoreContentService;
import com.scpii.universal.util.FileUploadUtil;
import com.scpii.universal.util.ImgAndFilePath;

/**
 * @author L
 * @version 1.0
 * @description 客户端更多功能管理
 */
@Controller
@Scope("prototype")
public class ClientMoreAction extends BaseAction {

	private static final long serialVersionUID = 1962796557117190690L;
	@Resource
	private MoreContentService moreContentService;
	@Resource
	private AppUpdateService appUpdateService;
	@Resource
	private AppIconService appIconService;
	private List<ClientMoreDto> lstClientMores;
	private String loadingUrl;
	private Integer loadingType = 0;
	private Integer loadingIconId;

	public String goInit() {
		int type = Integer.parseInt(request.getParameter("type"));
		initPageDate(type);
		lstClientMores = moreContentService.listMoreContentByCon(getUserInfo().getAppId(), type);
		return SUCCESS;
	}
	
	public String doClientMoreSave() throws UnsupportedEncodingException {
		int id = Integer.parseInt(request.getParameter("id"));
		String content = request.getParameter("content");
		content = URLDecoder.decode(URLDecoder.decode(content,"utf-8"),"utf-8");
		moreContentService.updateMoreContent(id, content);
		return null;
	}
	
	/**
	 * 删除更多内容
	 * @return
	 * @throws IOException
	 */
	public String deleteMoreContent() throws IOException {
		int id = Integer.parseInt(request.getParameter("id"));
		moreContentService.delete(id);
		jsonOut(GenericConstants.OPERATION_SUCCESS);
		return null;
	}

	private void initPageDate(int type) {
		switch (type) {
		case GenericConstants.BASIC_TYPE_PHONE:
			request.setAttribute("title", "一键拨打");
			request.setAttribute("contentName", "电话号码");
			break;
		case GenericConstants.BASIC_TYPE_LINK:
			request.setAttribute("title", "外链地址");
			request.setAttribute("contentName", "url地址");
			break;
		case GenericConstants.BASIC_TYPE_SHARE:
			request.setAttribute("title", "客户端分享");
			request.setAttribute("contentName", "客户端分享语");
			break;
		default:
			break;
		}
	}
	
	/**
	 * loading页面管理
	 * @return
	 */
	public String goViewLoading() {
		AppUpdateTab iosAppUpd = appUpdateService.getVersionUpdateInfo(
				getUserInfo().getAppId(), null, GenericConstants.OPERATION_SYS_IOS);
		loadingIconId = iosAppUpd.getIntLoadingIconId();
		if(loadingIconId == null){
			loadingIconId = 0;
			loadingUrl = "";
			return SUCCESS;
		}
		loadingUrl = appIconService.getIconURLById(loadingIconId);
		if ("mp4".equalsIgnoreCase(loadingUrl.substring(loadingUrl.lastIndexOf(".") + 1))) {
			loadingType = 1;
		}
		return SUCCESS;
	}
	
	public String changeLoading() {
		String loadingIconUrl = request.getParameter("url");
		if (StringUtils.isBlank(loadingIconUrl))
			return null;
		String srcFile = getWebAppsRealPath() + loadingIconUrl.replace(getServerURL(),"").replace("/", File.separator);
		if (!new File(srcFile).exists())
			return null;
		String relativePath = getImgBasePath() + ImgAndFilePath.ENTERP_LOADING + File.separator + FilenameUtils.getName(loadingIconUrl);
		String targetPath = getWebAppsRealPath() + relativePath;
		if (srcFile.equals(targetPath))
			return null;
		FileUploadUtil.copy(srcFile, targetPath);
		appIconService.updateLoadingIcon(loadingIconId, relativePath.replaceAll("\\\\", "/"));
		File file = new File(srcFile);
		file.delete();
		return null;
	}

	public List<ClientMoreDto> getLstClientMores() {
		return lstClientMores;
	}

	public void setLstClientMores(List<ClientMoreDto> lstClientMores) {
		this.lstClientMores = lstClientMores;
	}

	public String getLoadingUrl() {
		return loadingUrl;
	}

	public void setLoadingUrl(String loadingUrl) {
		this.loadingUrl = loadingUrl;
	}

	public Integer getLoadingType() {
		return loadingType;
	}

	public void setLoadingType(Integer loadingType) {
		this.loadingType = loadingType;
	}

	public Integer getLoadingIconId() {
		return loadingIconId;
	}

	public void setLoadingIconId(Integer loadingIconId) {
		this.loadingIconId = loadingIconId;
	}

}
